<?php

namespace App\Http\Controllers\Mobile;

use App\Models\Plan;
use App\Models\Shop;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class XunDianJingDuController extends Controller
{
    /**
     * 巡店经度报表查看
     *
     * @author Eric
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function XunDianBaoBiao(Request $request){

        // $year = $request->input('year',date('Y'));

        // $month = $request->input('month',date('m'));

        $start = $request->input('start',date('Y-m-d')).' 00:00:00';

        $end = $request->input('end',date('Y-m-d')).' 23:59:59';

        $PlanQuery = Plan::query();

        // 条件
        $plans = $PlanQuery->where('user_id', Auth::id())->whereBetween('start',[$start,$end])->get();


        // 计划总数
        $sum_num = $plans->count();

        //
        $group = $plans->groupBy('status');

        // 已完成数量
        $yi_wan_cheng = optional($group->get('1'))->count();

        // 未完成数量
        $wei_wan_cheng = optional($group->get('0'))->count();


        $datas = [
            'zong_shu' => $sum_num,
            'yi_wan_cheng' => $yi_wan_cheng == '' ? 0 : $yi_wan_cheng,
            'wei_wan_cheng' => $wei_wan_cheng == '' ? 0 : $wei_wan_cheng,
            'yi_wan_cheng_bai' => $yi_wan_cheng == '' ? 0 : ceil($yi_wan_cheng/$sum_num*100),
            'wei_wan_cheng_bai' => $wei_wan_cheng == '' ? 0 : ceil($wei_wan_cheng/$sum_num*100),
        ];

        return $this->success($datas);

    }

	/**
	 * Date: 02/04/2018
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function XunDianXiangQing(Request $request)
    {
        $start = $request->input('start',date('Y-m-d').' 00:00:00');

        $end = $request->input('end',date('Y-m-d').' 23:59:59');
        $paginate = $request->get('paginate', 10);

        return $this->success(Plan::with(['shop', 'XunDianJiLu'])->where('user_id', Auth::id())
            ->whereBetween('start',[$start,$end])
            ->paginate($paginate));
    }
}
